<?php
/**
*
* @author Mihold (Mykhailo Kutsenko) miholdk@gmail.com
* @package - GoodPost
* @version $Id$
* @copyright (c) 2012 Mykhailo Kutsenko
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$user->add_lang('mods/goodpost');

/**
* Check user vote status
* Return status:
* 		 2 - voting closed or system haven't post with this number (overal error)
* 		 1 - positive vote present
* 		 0 - vote not set
* 		-1 - negative vote present
*/
function mod_goodpost_vote_status($user_id, $post_id)
{
	global $db, $config;
	
	/**
	* To do:
	* 		- check for fixed voting
	*/
	$sql = 'SELECT post_time
		FROM ' . POSTS_TABLE . "
		WHERE post_id = $post_id";
	$result = $db->sql_query($sql);
	$post_time = (int) $db->sql_fetchfield('post_time');
	$db->sql_freeresult($result);
	// Check voting time period
	if (($config['mod_goodpost_days_limit'] == 0) or ((time() - $post_time) < ($config['mod_goodpost_days_limit'] * 86400)))
	{
		$sql = 'SELECT vote_val
			FROM ' . GOODPOST_TABLE . "
			WHERE (post_id = $post_id) AND (user_id = $user_id)";
		$result = $db->sql_query($sql);
		$post_vote = (int) $db->sql_fetchfield('vote_val');
		$db->sql_freeresult($result);
	}
	else
	{
		$post_vote = 2;
	}
	return $post_vote;
}

/**
* Voting actions
* Result: post ID on success or 0 if failed
*/
function mod_goodpost_action($user_id, $action)
{
	global $config;
	
	if ((strlen($action) > 1) && ($config['mod_goodpost_active'] == 1))
	{
		global $db;
		
		// Get action and post id
		$operation	= substr($action, 0, 1);
		$post_id	= (int) substr($action, 1);
		
		// Check post id
		if ($post_id > 0)
		{
			$sql = 'SELECT post_id
				FROM ' . POSTS_TABLE . "
				WHERE post_id = $post_id";
			$result = $db->sql_query($sql);
			$post_id = (int) $db->sql_fetchfield('post_id');
			$db->sql_freeresult($result);
		}
		else
		{
			$post_id = false;
		}

		if (!$post_id)
		{
			trigger_error('MOD_GOODPOST_NO_POST');
		}
		
		// Do actions
		switch ($operation)
		{
			// Action - Rate up
			case 'u':
				//Get current status
				$gp_status = mod_goodpost_vote_status($user_id, $post_id);
				if ($gp_status < 1)
				{
					// Make a statment
					$sql = 'DELETE FROM ' . GOODPOST_TABLE . "
						WHERE (post_id = $post_id) AND (user_id = $user_id)";
					$db->sql_query($sql);
					if ($gp_status == 0)
					{
						$insert_ary = array(
							'post_id'	=> $post_id,
							'user_id'	=> $user_id,
							'vote_date'	=> time(),
							'vote_val'	=> 1);
						$sql = 'INSERT INTO ' . GOODPOST_TABLE . ' ' . $db->sql_build_array('INSERT', $insert_ary);;
						$db->sql_query($sql);
					}
				}
			break;
			
			// Action - Rate down
			case 'd':
				//Get current status
				$gp_status = mod_goodpost_vote_status($user_id, $post_id);
				if (($gp_status == 1) || ($gp_status == 0))
				{
					// Make a statment
					$sql = 'DELETE FROM ' . GOODPOST_TABLE . "
						WHERE (post_id = $post_id) AND (user_id = $user_id)";
					$db->sql_query($sql);
					if ($gp_status == 0)
					{
						$insert_ary = array(
							'post_id'	=> $post_id,
							'user_id'	=> $user_id,
							'vote_date'	=> time(),
							'vote_val'	=> -1);
						$sql = 'INSERT INTO ' . GOODPOST_TABLE . ' ' . $db->sql_build_array('INSERT', $insert_ary);;
						$db->sql_query($sql);
					}
				}
			break;
			
			default:
				trigger_error('MOD_GOODPOST_WRONG_FORMAT');
			break;
		}
	}
	return $post_id;
}

/**
* Check post status
* Return status:
* 		 1 - positiv result
* 		 0 - result not set or error
* 		-1 - negative result
*/
function mod_goodpost_post_status($post_id)
{
	global $db, $config;
	
	$post_status = 0;
	//Get post info
	/*$sql = 'SELECT post_time
		FROM ' . POSTS_TABLE . "
		WHERE post_id = $post_id";
	$result = $db->sql_query($sql);
	$post_info = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);*/
	
	// Check voting time period
	//$voting_timelimit = (time() - $post_info['post_time']) < ($config['mod_goodpost_days_limit'] * 86400);
	
	//Get voting info
	/**
	* Think about:
	*	- coefficients for topic creator, moderators and admins
	*/
	$sql = 'SELECT SUM(vote_val) AS vresult, COUNT(post_id) AS vcount
		FROM ' . GOODPOST_TABLE . "
		WHERE post_id = $post_id";
	$result = $db->sql_query($sql);
	$voting_info = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	if ($voting_info['vresult'])
	{
		$post_status = $voting_info['vresult'];
		$post_status = $post_status / abs($post_status);
	}
	
	return $post_status;
}

// Output post information
function mod_goodpost_postrow($user_id, $post_num, $post_id, $poster_id, $forum_id, $topic_id)
{
	//global $db, $user, $poster_id, $postrow, $row, $phpEx, $topic_data, $phpbb_root_path, $config, $forum_id, $max_post_thanks, $auth, $poster_list_count;
	global $postrow, $phpEx, $phpbb_root_path, $config;
	
	/**
	* Checking:
	*		- MOD activated?
	*		- it's not first post?
	*		- are you not author of this post?
	* To do:
	* 		- finaling results
	*/
	if (!empty($postrow) && ($config['mod_goodpost_active'] == 1) && ($post_num > 1))
	{
		if ($user_id != $poster_id)
		{
			// Get permision rights
			// ???... thinking about this
			
			// Get curent state
			$gp_status	= mod_goodpost_vote_status($user_id, $post_id);
			$allow_up	= $gp_status < 1;
			$allow_down	= ($gp_status == 0) || ($gp_status == 1);
			
			// Fill template vars
			$postrow = array_merge($postrow, array(
				'U_RATE_UP'		=> ($allow_up) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;goodpost=u$post_id") : '',
				'U_RATE_DOWN'	=> ($allow_down) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;goodpost=d$post_id") : '',
			));
		}
		// Get post status
		$post_state = mod_goodpost_post_status($post_id);
		
		$post_state = ($post_state > 0) ? 'good' : (($post_state < 0) ? 'bad' : '');
		if ($post_state)
		{
			$postrow = array_merge($postrow, array(
				'GOODPOST_STYLE'	=> $post_state,
				'GOODPOST_STYLE1'	=> $config['mod_goodpost_bg'],
				'GOODPOST_STYLE2'	=> $config['mod_goodpost_title'],
				'GOODPOST_STYLE3'	=> $config['mod_goodpost_line'],
				'GOODPOST_ICON_IMG'	=> ($config['mod_goodpost_icon'] == 1) ? '<img src="' . $phpbb_root_path . 'images/mod_goodpost/icon_mod_goodpost_' . $post_state . '.png">' : '',
			));
		}
	}
}